﻿using Microsoft.Scripting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public class R
    {
        public static (int, int) 取匹配位置及长度(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return (-1, -1);
            if (string.IsNullOrEmpty(rule)) return (-1, -1);
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var match = reg.Match(srcText);
            return (match.Index, match.Length);
        }

        public static bool 匹配(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return false;
            if (string.IsNullOrEmpty(rule)) return false;
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var match = reg.Match(srcText);
            return match.Success;
        }

        public static string 取匹配串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return null;
            if (string.IsNullOrEmpty(rule)) return null;
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var match = reg.Match(srcText);
            return match.Success ? match.Value : null;
        }

        public static string 取匹配片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配串(srcText, rule, ignorecase, multiLines);
        }

        public static string 取匹配文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配串(srcText, rule, ignorecase, multiLines);
        }

        public static string 取匹配字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配串(srcText, rule, ignorecase, multiLines);
        }

        public static string 取匹配值(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配串(srcText, rule, ignorecase, multiLines);
        }

        public static string 取前侧(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return null;
            if (string.IsNullOrEmpty(rule)) return null;
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var match = reg.Match(srcText);
            return match.Success ? srcText.Substring(0, match.Index) : null;
        }

        public static string 取前侧文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取前侧(srcText, rule, ignorecase, multiLines);
        }

        public static string 取前侧片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取前侧(srcText, rule, ignorecase, multiLines);
        }

        public static string 取前侧字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取前侧(srcText, rule, ignorecase, multiLines);
        }

        public static string 取后侧(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return null;
            if (string.IsNullOrEmpty(rule)) return null;
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var matches = reg.Matches(srcText);
            if (matches.Count <= 0) return null;

            return srcText.Substring(matches[matches.Count - 1].Index + matches[matches.Count - 1].Length);
        }

        public static string 取后侧文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取后侧(srcText, rule, ignorecase, multiLines);
        }

        public static string 取后侧片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取后侧(srcText, rule, ignorecase, multiLines);
        }

        public static string 取后侧字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取后侧(srcText, rule, ignorecase, multiLines);
        }

        public static string[] 取匹配集(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return null;
            if (string.IsNullOrEmpty(rule)) return null;
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var matches = reg.Matches(srcText);
            if (matches.Count > 0)
            {
                string[] result = new string[matches.Count];
                for (int i = 0; i < matches.Count; i++)
                {
                    result[i] = matches[i].ToString();
                }
                return result;
            }
            else return null;
        }

        public static string[] 取所有匹配字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配集(srcText, rule, ignorecase, multiLines);
        }

        public static string[] 取所有匹配片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配集(srcText, rule, ignorecase, multiLines);
        }

        public static string[] 取所有匹配文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配集(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及前侧(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return (null, null);
            if (string.IsNullOrEmpty(rule)) return (null, null);
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var match = reg.Match(srcText);
            return match.Success ? (match.Value, srcText.Substring(0, match.Index)) : (null, null);
        }

        public static (string, string) 取匹配值及前侧文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及前侧(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及前侧字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及前侧(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及前侧片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及前侧(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及后侧(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            if (string.IsNullOrEmpty(srcText)) return (null, null);
            if (string.IsNullOrEmpty(rule)) return (null, null);
            RegexOptions options = RegexOptions.None;
            if (multiLines)
            {
                options |= RegexOptions.Multiline;
            }

            if (ignorecase)
            {
                options |= RegexOptions.IgnoreCase;
            }
            var reg = new Regex(rule, options);

            var matches = reg.Matches(srcText);
            if (matches.Count <= 0) return (null, null);

            return (matches[matches.Count - 1].Value, srcText.Substring(matches[matches.Count - 1].Index + matches[matches.Count - 1].Length));
        }

        public static (string, string) 取匹配值及后侧文本(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及后侧(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及后侧片段(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及后侧(srcText, rule, ignorecase, multiLines);
        }

        public static (string, string) 取匹配值及后侧字符串(string srcText, string rule, bool ignorecase = true, bool multiLines = false)
        {
            return 取匹配值及后侧(srcText, rule, ignorecase, multiLines);
        }

    }
}
